package com.meepo.framework.design.helper

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition

object ImageLoader {

    @JvmStatic
    @JvmOverloads
    fun loadImage(context: Context, url: Any?, imageView: ImageView,
                  size: Int = 0,
                  requestOptions: RequestOptions = RequestOptions().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL),
                  transitionOptions: DrawableTransitionOptions = DrawableTransitionOptions().crossFade(0),
                  listener: RequestListener<Drawable>? = null) {

        val builder = Glide.with(context).load(url)
                .apply(requestOptions)
                .transition(transitionOptions)
                .listener(listener)

        if (size > 0) builder.override(Utils.dp2px(context, size))

        builder.into(imageView)
    }

    @JvmStatic
    fun loadImageBackground(context: Context, url: Any, view: View) {

        Glide.with(context).load(url).into(object : CustomTarget<Drawable>() {
            override fun onLoadCleared(placeholder: Drawable?) {
            }

            override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                view.background = resource
            }
        })
    }
}